package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.HotArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.vos.HotArticleVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.pojos.WmChannel;
import org.joda.time.DateTime;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class HotArticleServiceImpl implements HotArticleService {


    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private IWemediaClient wemediaClient;

    @Autowired
    private CacheService cacheService;

    /**
     * 计算热点文章
     */
    @Override
    public void computeHotArticle() {
        //设置前面5天的时间
        Date date = DateTime.now().minusDays(5).toDate();
        //查询前面5天的所有的文章数据
        List<ApArticle> apArticleList = apArticleMapper.findArticleListByDays(date);
        if (apArticleList != null && apArticleList.size() > 0){
            //计算所有的文章的得分
            List<HotArticleVo> voList = computeHotArticleScore(apArticleList);
            //把文章缓存到redis(每个文章都要缓存,推荐需要缓存)
            cacheTagToRedis(voList);
        }
     }

    /**
     * 缓存到redis
     * @param voList
     */
    private void cacheTagToRedis(List<HotArticleVo> voList) {
        //查询出每个频道的文章
        if (voList != null && voList.size() > 0) {
            ResponseResult channels = wemediaClient.getChannels();
            if (channels.getCode().equals(200)) {
                WmChannel wmChannel = JSON.parseObject(JSON.toJSONString(channels.getData()), WmChannel.class);
                List<HotArticleVo> collect = voList.stream().filter(vo -> vo.getChannelId()
                                .equals(wmChannel.getId()))
                        .sorted(Comparator.comparing(HotArticleVo::getScore).reversed())
                        .limit(30)
                        .collect(Collectors.toList());
                cacheService.set(ArticleConstants.HOT_ARTICLE_FIRST_PAGE + wmChannel.getId() , JSON.toJSONString(collect));
            }
            //给推荐频道设置缓存
            List<HotArticleVo> list = voList.stream()
                    .sorted(Comparator.comparing(HotArticleVo::getScore).reversed())
                    .limit(30).collect(Collectors.toList());
            cacheService.set(ArticleConstants.HOT_ARTICLE_FIRST_PAGE+ArticleConstants.DEFAULT_TAG, JSON.toJSONString(list));
        }
    }

    /**
     * 计算得分
     * @param apArticleList
     * @return
     */
    private List<HotArticleVo> computeHotArticleScore(List<ApArticle> apArticleList) {
        List<HotArticleVo> voList = new ArrayList<>(apArticleList.size());
        for (ApArticle article : apArticleList) {
            HotArticleVo vo = new HotArticleVo();
            BeanUtils.copyProperties(article,vo);
            Integer sore = computeScore(article);
            vo.setScore(sore);
            voList.add(vo);
        }
        return voList;
    }

    /**
     * 计算具体得分
     * @param article
     * @return
     */
    private Integer computeScore(ApArticle article) {
        Integer score = 0;
        if(article.getLikes() != null){
            score += article.getLikes()* ArticleConstants.HOT_ARTICLE_LIKE_WEIGHT;
        }

        if(article.getViews() != null){
            score += article.getViews();
        }

        if(article.getComment() != null){
            score += article.getComment()* ArticleConstants.HOT_ARTICLE_COMMENT_WEIGHT;
        }

        if(article.getCollection() != null){
            score += article.getCollection()* ArticleConstants.HOT_ARTICLE_COLLECTION_WEIGHT;
        }

        return score;
    }

}

